package com.study.samsung.Dao;

import com.study.samsung.Entity.Collect;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class CenterDao {
    private SQLHelper helper = new SQLHelper();

    public List<Collect> selectAllCol(Integer uid){
        String sql = "SELECT * FROM commodity a LEFT JOIN collect c ON a.sid = c.sid WHERE uid=? LIMIT 0,3";
        return helper.query(sql,new CollectRowMpper(),uid);
    }
    public Collect selectOne(Integer uid,Integer sid){
        String sql = "SELECT * FROM commodity a LEFT JOIN collect c ON a.sid = c.sid WHERE c.uid=? AND c.sid=?";
        return helper.one(sql,new CollectRowMpper(),uid,sid);
    }
    public List<Collect> selectAll(Integer uid){
        String sql = "SELECT * FROM commodity a LEFT JOIN collect c ON a.sid = c.sid WHERE uid=?";
        return helper.query(sql,new CollectRowMpper(),uid);
    }

    class CollectRowMapperParent implements RowMapper<Collect> {

        @Override
        public Collect map(ResultSet rs) throws SQLException {
            return new Collect(rs.getInt("cid"),
                    rs.getDate("ctime"),
                    rs.getInt("sid")
            );
        }
    }
    class CollectRowMpper extends CollectRowMapperParent {
        @Override
        public Collect map(ResultSet rs) throws SQLException {
            Collect collect = super.map(rs);
            collect.setLid(rs.getInt("lid"));
            collect.setSname(rs.getString("sname"));
            collect.setUrl(rs.getString("url"));
            collect.setSprice(rs.getBigDecimal("sprice"));
            return collect;
        }
    }
}
